package com.pkx.guide.info.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pkx.guide.common.util.StringUtils;
import com.pkx.guide.info.entity.TInfoGuideImg;
import com.pkx.guide.info.entity.TInfoGuideStatus;
import com.pkx.guide.info.entity.TInfoGuideStatusHistory;
import com.pkx.guide.info.mapper.TInfoGuideMapper;
import com.pkx.guide.info.mapper.TInfoGuideStatusMapper;
import com.pkx.guide.info.service.TInfoGuideImgService;
import com.pkx.guide.info.service.TInfoGuideStatusService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 诱导屏状态表 服务实现类
 *
 * @author sunsy
 * @since 2023-12-20
 */
@Service
public class TInfoGuideStatusServiceImpl extends ServiceImpl<TInfoGuideStatusMapper, TInfoGuideStatus> implements TInfoGuideStatusService {

    @Resource
    private TInfoGuideImgService tInfoGuideImgService;
    @Resource
    private TInfoGuideMapper tInfoGuideMapper;
    /**
     * 分页列表
     *
     * @param tInfoGuideStatus
     * @return
     */
    @Override
    public IPage<TInfoGuideStatus> pageList(Page page, TInfoGuideStatus tInfoGuideStatus) {
        LambdaQueryWrapper<TInfoGuideStatus> lambdaQueryWrapper = new QueryWrapper<TInfoGuideStatus>().lambda();
        return page(page, lambdaQueryWrapper);
    }

    /**
     * 列表查询
     *
     * @param tInfoGuideStatus
     * @return
     */
    @Override
    public List<TInfoGuideStatus> listByEntity(TInfoGuideStatus tInfoGuideStatus) {
        LambdaQueryWrapper<TInfoGuideStatus> lambdaQueryWrapper = new QueryWrapper<TInfoGuideStatus>().lambda();
        return list(lambdaQueryWrapper);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public TInfoGuideStatus selectById(Integer id) {
        return getById(id);
    }

    /**
     * 保存
     *
     * @param tInfoGuideStatus
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveTInfoGuideStatus(TInfoGuideStatus tInfoGuideStatus) {
        return save(tInfoGuideStatus);
    }

    /**
     * 保存
     *
     * @param tInfoGuideStatus
     * @return
     */
    @Override
    public Boolean saveOrUpdateGuideStatus(TInfoGuideStatus tInfoGuideStatus){
        return saveOrUpdate(tInfoGuideStatus);
    }

    /**
     * 修改
     *
     * @param publishDetail
     * @return
     */
    @Override
    public Boolean updateGuideStatus(String publishDetail){
        if (StringUtils.isEmpty(publishDetail)){
            return false;
        }
        JSONArray jsonArray = JSONArray.parseArray(publishDetail);
        List<TInfoGuideStatus> guideStatusList = new ArrayList<>();
        TInfoGuideStatus tInfoGuideStatus = null;
        for (Object o : jsonArray) {
            JSONObject item = (JSONObject) o;
            String ip = item.getString("ip");
            String prgid = item.getString("prgid");
            tInfoGuideStatus = new TInfoGuideStatus();
            tInfoGuideStatus.setGuidIp(ip);
            tInfoGuideStatus.setImgNum(Integer.valueOf(prgid));
            guideStatusList.add(tInfoGuideStatus);
            // baseMapper.updateGuideStatus(ip, Integer.valueOf(prgid));
        }
        return baseMapper.updateBatchGuideStatus(guideStatusList);
    }
//@Resource
//private TInfoGuideStatusHistoryMapper tInfoGuideStatusHistoryMapper;
    /**
     * 修改
     *
     * @param data
     * @return
     */
    @Override
    public Boolean updateGuideStatus(JSONArray data){
        if (data == null || data.size() == 0){
            return false;
        }
        List<TInfoGuideImg> guideImgList = tInfoGuideImgService.listByEntity(new TInfoGuideImg());
        if (guideImgList == null || guideImgList.size() == 0){
            return false;
        }
        List<Integer> imgNumList = guideImgList.stream().map(TInfoGuideImg::getImgNum).collect(Collectors.toList());
        if (imgNumList.size() == 0){
            return false;
        }
        List<TInfoGuideStatus> guideStatusList = new ArrayList<>();
        TInfoGuideStatus tInfoGuideStatus = null;
        for (Object o : data) {
            JSONObject item = (JSONObject) o;
            String ip = item.getString("ip");
            String ret = item.getString("ret");
            if (!StringUtils.isNumeric(ret)){
                continue;
            }
            tInfoGuideStatus = new TInfoGuideStatus();
            tInfoGuideStatus.setGuidIp(ip);
            tInfoGuideStatus.setImgNum(Integer.valueOf(ret));
            TInfoGuideStatusHistory tInfoGuideStatusHistory = new TInfoGuideStatusHistory();
            tInfoGuideStatusHistory.setGuidIp(ip);
            tInfoGuideStatusHistory.setImgNum(Integer.valueOf(ret));
            if (!imgNumList.contains(Integer.valueOf(ret))){
               if (Integer.valueOf(ret)==255){
                   tInfoGuideStatus.setOfflineStatus("3");
                   log.error("ip:{},回读失败."+ip);
               }else {
                   tInfoGuideStatus.setOfflineStatus("2");
                   log.error("ip:{},节目列表."+ip);
               }

            }

            guideStatusList.add(tInfoGuideStatus);
        }
//        tInfoGuideStatusHistoryMapper.insertBatch(guideStatusList);
        return baseMapper.updateBatchGuideStatus(guideStatusList);
    }

    /**
     * 修改
     *
     * @param tInfoGuideStatus
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(TInfoGuideStatus tInfoGuideStatus) {
        return updateById(tInfoGuideStatus);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Integer id) {
        if (id == null){
            return false;
        }
        LambdaUpdateWrapper<TInfoGuideStatus> lambdaUpdateWrapper = new UpdateWrapper<TInfoGuideStatus>().lambda()
            .eq(TInfoGuideStatus::getGuideId, id)
            .set(TInfoGuideStatus::getDelFlg, 1);
        return update(lambdaUpdateWrapper);
    }

//    @Override
//    public List<TInfoGuideStatus> getOfflineDevice() {
//        LambdaQueryWrapper<TInfoGuideStatus> eq = new QueryWrapper<TInfoGuideStatus>().lambda()
//                .eq(TInfoGuideStatus::getOfflineStatus, "1")
//                .eq(TInfoGuideStatus::getDelFlg, 0);
//        List<TInfoGuideStatus> statusList = list(eq);
//        List<TInfoGuide> infoGuides = tInfoGuideMapper.selectList(new QueryWrapper<TInfoGuide>().lambda()
//                .eq(TInfoGuide::getDelFlg, 0));
//
//
//        Map<String, TInfoGuide> guideIpMap = infoGuides.stream()
//                .collect(Collectors.toMap(
//                        TInfoGuide::getGuidIp,
//                        guide -> guide,
//                        (existing, replacement) -> existing
//                ));
//        for (TInfoGuideStatus status : statusList) {
//            String ip = status.getGuidIp();
//            TInfoGuide matchedGuide = guideIpMap.get(ip);
//            if (matchedGuide != null) {
//                status.setTInfoGuide(matchedGuide);
//            }
//        }
//        return statusList ;
//    }
}
